//
//  TMGSettingViewController.m
//  TMGold
//
//  Created by Mac on 2017/12/8.
//  Copyright © 2017年 tangmu. All rights reserved.
//

#import "TMGSettingViewController.h"
#import "TMGLoginViewController.h"
#import "TMGSuggestViewController.h"
#import "TMGAboutUsViewController.h"
#import "TMGGodlAlertViewController.h"
#import "TMGAccountSafeViewController.h"
@interface TMGSettingViewController ()
@property(nonatomic, strong) NSArray  <NSArray*>                   *dataSouce;
@end

@implementation TMGSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"设置";
   
    [self configTableFooter];
    [self reloadData];
    // Do any additional setup after loading the view.
}

- (void)configTableFooter{
    __weak typeof(self) weakSelf =  self;
    UIView * footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 100)];
    footer.backgroundColor = self.tableView.backgroundColor;
    STButton * confimButton = [[STButton alloc] initWithFrame:CGRectMake(30, 0, UIScreenWidth - 60 , 44)
                                                        title:@"退出登录"
                                                   titleColor:[UIColor whiteColor]
                                                    titleFont:18
                                                 cornerRadius:10
                                              backgroundColor:TM_orangeColor
                                              backgroundImage:nil
                                                        image:nil];
    [confimButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedLogoutButton];
    }];
    confimButton.st_centerY = 50;
    confimButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [footer addSubview:confimButton];
    self.tableView.tableFooterView = footer;
}
- (void)reloadData{
    

    NSArray * sectionOne = @[
                             [[STTableEasyModel alloc] initWithTextString:@"账号安全" detailString:@""],
                             [[STTableEasyModel alloc] initWithTextString:@"金价提醒" detailString:@""]];
    
    NSArray * sectionTwo = @[
                             [[STTableEasyModel alloc] initWithTextString:@"关于我们" detailString:@""],
                             [[STTableEasyModel alloc] initWithTextString:@"意见反馈" detailString:@""]
                             ];
    self.dataSouce = @[sectionOne,sectionTwo];
    [self.tableView reloadData];
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.dataSouce.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSouce[section].count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 44;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 24;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseIdentifier];
        
    }
    cell.textLabel.textColor = UIColorFromRGBA(0x333333);
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    cell.detailTextLabel.font = [UIFont systemFontOfSize:13];
    cell.detailTextLabel.textColor = UIColorFromRGBA(0x999999);
    
    STTableEasyModel * model = self.dataSouce[indexPath.section][indexPath.row];
    cell.textLabel.text = model.textString;
    cell.detailTextLabel.text = model.detailString;
    if (model.accessoryView) {
        cell.accessoryView = model.accessoryView;
    }else{
        cell.accessoryType = model.accessoryType;
    }
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    STTableEasyModel * model = self.dataSouce[indexPath.section][indexPath.row];
    if ([model.textString isEqualToString:@"意见反馈"]) {
        [self.navigationController pushViewController:[TMGSuggestViewController new] animated:YES];
    }
    if ([model.textString isEqualToString:@"关于我们"]) {
        [self.navigationController pushViewController:[TMGAboutUsViewController new] animated:YES];
    }
    if ([model.textString isEqualToString:@"金价提醒"]) {
        [self.navigationController pushViewController:[TMGGodlAlertViewController new] animated:YES];
    }
    if ([model.textString isEqualToString:@"账号安全"]) {
        [self.navigationController pushViewController:[TMGAccountSafeViewController new] animated:YES];
    }
    
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}
- (void)onSelectedLogoutButton{
    [self showAlertCancelAndConfirm:@"确定要退出登录吗?" andWithBlock:^(NSString *name) {
        if ([name isEqualToString:@"确认"]) {
            [self sendLogOutRequest];
        }
    }];
    
}
- (void)sendLogOutRequest{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    NSString * url = [NSString stringWithFormat:@"%@%@",service_header,@"/gold/public/api/personal/logout"];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    if ([STUserManger defult].token.length) {
        [dic setObject:[STUserManger defult].token forKey:@"token"];
    }
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showSuccessWithStatus:@"退出登录成功"];
                                                 [[STUserManger defult] removeUserPreferce];
                                                 STNavigationController * nav = [[STNavigationController alloc] initWithRootViewController:[TMGLoginViewController new]];
                                                 [UIApplication sharedApplication].keyWindow.rootViewController = nav;
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}
@end
